package cn.ysym.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Administrator on 2016/12/14 0014.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, "ysym.db", null, 210407);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //系统设置数据表
        String sql = "CREATE TABLE setting (settingType VARCHAR (32) PRIMARY KEY NOT NULL, settingValue VARCHAR (128))";
        sqLiteDatabase.execSQL(sql);
        sql = "INSERT INTO setting (settingType, settingValue) VALUES ('userName', 'test')";
        sqLiteDatabase.execSQL(sql);
        sql = "INSERT INTO setting (settingType, settingValue) VALUES ('userPass', '')";
        sqLiteDatabase.execSQL(sql);
        sql = "INSERT INTO setting (settingType, settingValue) VALUES ('loginType', '0')";
        sqLiteDatabase.execSQL(sql);
        sql = "INSERT INTO setting (settingType, settingValue) VALUES ('sessionKey', '')";
        sqLiteDatabase.execSQL(sql);
        sql = "INSERT INTO setting (settingType, settingValue) VALUES ('urlPath', 'http://ysym.cn/yunma/scanner/')";
        sqLiteDatabase.execSQL(sql);
        //产品数据表
        sql = "CREATE TABLE product (productId VARCHAR (16) PRIMARY KEY NOT NULL, productName VARCHAR (64) NOT NULL, productNamePy VARCHAR (32))";
        sqLiteDatabase.execSQL(sql);
        //经销商数据表
        sql = "CREATE TABLE dealer (dealerId VARCHAR (16) PRIMARY KEY NOT NULL, dealerName VARCHAR (128) NOT NULL, dealerNamePy VARCHAR (32))";
        sqLiteDatabase.execSQL(sql);
        //促销数据表
        sql = "CREATE TABLE promotion (promotionId VARCHAR (16) PRIMARY KEY, promotionName VARCHAR (128), promotionNamePy VARCHAR (32))";
        sqLiteDatabase.execSQL(sql);
        //离线数据表
        sql = "CREATE TABLE offline_sum (id INTEGER PRIMARY KEY AUTOINCREMENT, account VARCHAR (32) NOT NULL, procType VARCHAR (1) NOT NULL, procDesc VARCHAR (64), procNum INTEGER NOT NULL, upSuccNum INTEGER, upFailNum INTEGER)";
        sqLiteDatabase.execSQL(sql);
        sql = "CREATE TABLE offline_stock (id INTEGER PRIMARY KEY AUTOINCREMENT, sumId INTEGER NOT NULL, productId VARCHAR (16) NOT NULL, bigCode VARCHAR (16), code VARCHAR (32) NOT NULL, virtualBig VARCHAR (1), upSucc VARCHAR (1) NOT NULL, scanTime VARCHAR(16))";
        sqLiteDatabase.execSQL(sql);
        sql = "CREATE TABLE offline_shipping (id INTEGER PRIMARY KEY AUTOINCREMENT, sumId INTEGER NOT NULL, dealerId VARCHAR (16), orderId VARCHAR (32), shippingId VARCHAR (32), productId VARCHAR (16), promotionId VARCHAR (16), code VARCHAR (32) NOT NULL, virtualBig VARCHAR (1), upSucc VARCHAR (1), scanTime VARCHAR(16))";
        sqLiteDatabase.execSQL(sql);
        sql = "CREATE TABLE shipping_order (id INTEGER PRIMARY KEY AUTOINCREMENT, orderNo VARCHAR (128))";
        sqLiteDatabase.execSQL(sql);

        //调货单号数据表
        sql = "CREATE TABLE shipping_move (moveId INTEGER (16) PRIMARY KEY NOT NULL, toDealerName VARCHAR (128) NOT NULL)";
        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS setting");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS product");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS dealer");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS promotion");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS offline_sum");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS offline_stock");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS offline_shipping");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS shipping_order");
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS shipping_move");
        this.onCreate(sqLiteDatabase);
    }
}
